package com.bingxue.edu.framework.excel.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.bingxue.edu.common.enums.GenderEnum;
import com.bingxue.edu.common.enums.StudentClassEnum;
import io.micrometer.common.util.StringUtils;

import java.util.Objects;

/**
 * 学生行课状态转换器
 */
public class StudentClassStatusConverter implements Converter<StudentClassEnum> {
    @Override
    public Class<?> supportJavaTypeKey() {
        return StudentClassEnum.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    /**
     * 从excel读数据时候调用
     * 单元格数据转java数据
     * @param cellData Excel 单元格的数据，<?> 表示通配符，意味着可以接受任何类型的数据
     * @param contentProperty Excel 内容的元数据，例如列标题、格式等
     * @param globalConfiguration 一般是全局配置
     * @return
     */
    @Override
    public StudentClassEnum convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
                                     GlobalConfiguration globalConfiguration) {
        String value = cellData.getStringValue();

        if (value.contains("转出")) {
            return StudentClassEnum.CHANGE_CLASS;
        }
        if (value.contains("退班")) {
            return StudentClassEnum.CANCEL_CLASS;
        }
        if (value.contains("退课")) {
            return StudentClassEnum.CANCEL_LESSON;
        }
        if (value.contains("调出")) {
            return StudentClassEnum.CHANGE_LESSON;
        }
        if (value.contains("正常")) {
            return StudentClassEnum.NORMAL;
        }
        return StudentClassEnum.NORMAL;
    }

    /**
     * 写数据到excel里面
     * java数据转单元格数据
     * @param context 上下文对象,可以获取值
     * @return
     */
    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<StudentClassEnum> context) {
        StudentClassEnum value = context.getValue();
        if (Objects.equals(value, StudentClassEnum.CHANGE_CLASS)) {
            return new WriteCellData<>("转出");
        }
        if (Objects.equals(value, StudentClassEnum.CANCEL_CLASS)) {
            return new WriteCellData<>("退班");
        }
        if (Objects.equals(value, StudentClassEnum.CANCEL_LESSON)) {
            return new WriteCellData<>("退课");
        }
        if (Objects.equals(value, StudentClassEnum.CHANGE_LESSON)) {
            return new WriteCellData<>("调出");
        }
        if (Objects.equals(value, StudentClassEnum.NORMAL)) {
            return new WriteCellData<>("正常");
        }
        return new WriteCellData<>("未知");
    }

}
